<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:saxon="http://saxon.sf.net/"
  xmlns:f="livcos.org/ns/xslt/function"
  exclude-result-prefixes="xs saxon f">

  <xsl:variable name="projectUri" select="'/livcos.org/LivCos/data'"/>
	<xsl:variable name="projects" select="document(concat($projectUri, '/pm/egw/pm_projects.content'))/*/*"/>
	<xsl:variable name="trackers" select="document(concat($projectUri, '/pm/egw/tracker.content'))/*/*"/>
	<xsl:variable name="infologs" select="document(concat($projectUri, '/pm/egw/infolog.content'))/*/*"/>
  <xsl:variable name="links" select="document(concat($projectUri, '/pm/egw/links.content'))/*/*"/>
  <xsl:variable name="elementLinks" select="$links[link_app2 = 'projectmanager']"/>
  <xsl:variable name="projectLinks" select="$elementLinks[link_app1 = 'projectmanager']"/>
  <xsl:variable name="trackerLinks" select="$elementLinks[link_app1 = 'tracker']"/>
  <xsl:variable name="infoLogLinks" select="$elementLinks[link_app1 = 'infolog']"/>
  
  <xsl:template match="/">
    <projects>
      <xsl:apply-templates select="$projects[empty(index-of($projectLinks/link_id2, pm_id))]"/>
    </projects>
  </xsl:template>

  <xsl:template match="egw_pm_projects">
    <project id="project_{pm_id}" created="{f:secondsToDateTime(pm_created)}"
      priority="{pm_priority}">
      <xsl:apply-templates select="pm_modified"/>
      <xsl:apply-templates select="pm_completion"/>
      <label><xsl:value-of select="pm_number"/></label>
      <title><xsl:value-of select="pm_title"/></title>
      <xsl:apply-templates select="pm_real_start"/>
      <xsl:apply-templates select="pm_description"/>
      <xsl:variable name="pLinks" select="$projectLinks[link_id1 = current()/pm_id]"/>
      <xsl:apply-templates select="$projects[boolean(index-of($pLinks/link_id2, pm_id))]"/>
      <xsl:variable name="tLinks" select="$trackerLinks[link_id2 = current()/pm_id]"/>
      <xsl:apply-templates select="$trackers[boolean(index-of($tLinks/link_id1, tr_id))]"/>
      <xsl:variable name="iLinks" select="$infoLogLinks[link_id2 = current()/pm_id]"/>
      <xsl:apply-templates select="$infologs[boolean(index-of($iLinks/link_id1, info_id))]"/>
    </project>
  </xsl:template>
  
  <xsl:template match="egw_tracker">
    <xsl:variable name="n">
      <xsl:choose>
        <xsl:when test="tr_tracker = '6'">feature</xsl:when>
        <xsl:when test="tr_tracker = '7'">bug</xsl:when>
      </xsl:choose>
    </xsl:variable>
    <xsl:element name="{$n}">
      <xsl:attribute name="id"><xsl:value-of select="$n"/>_<xsl:value-of select="tr_id"/></xsl:attribute>
      <xsl:attribute name="created"><xsl:value-of select="f:secondsToDateTime(tr_created)"/></xsl:attribute>
      <xsl:attribute name="progress"><xsl:value-of select="tr_completion"/></xsl:attribute>
      <xsl:attribute name="priority"><xsl:value-of select="tr_priority"/></xsl:attribute>
      <xsl:apply-templates select="tr_modified | tr_closed"/>
      <title><xsl:value-of select="tr_summary"/></title>
      <xsl:apply-templates select="tr_description"/>
    </xsl:element>
  </xsl:template>
  
  <xsl:template match="egw_infolog">
    <xsl:element name="{info_type}">
      <xsl:attribute name="id"><xsl:value-of select="info_type"/>_<xsl:value-of select="info_id"/></xsl:attribute>
      <xsl:attribute name="progress"><xsl:value-of select="info_percent"/></xsl:attribute>
      <xsl:attribute name="priority"><xsl:value-of select="info_priority"/></xsl:attribute>
      <xsl:apply-templates select="info_datemodified"/>
      <title><xsl:value-of select="info_subject"/></title>
      <xsl:if test="info_type != 'note'">
        <xsl:apply-templates select="info_startdate"/>
      </xsl:if>
      <xsl:apply-templates select="info_des"/>
    </xsl:element>
  </xsl:template>
  
  <xsl:template match="pm_description | tr_description | info_des">
    <xsl:if test="normalize-space() != ''">
      <description>
        <xsl:value-of select="saxon:base64Binary-to-string(xs:base64Binary(.), 'UTF8')"/>
      </description>
    </xsl:if>
  </xsl:template>
  
  <xsl:template match="pm_real_start">
    <schedule id="schedule_{count(../preceding-sibling::egw_pm_projects) + 1}"
      start="{f:secondsToDateTime(.)}">
      <xsl:if test="../pm_real_end">
        <xsl:attribute name="end">
          <xsl:value-of select="f:secondsToDateTime(../pm_real_end)"/>
        </xsl:attribute>
      </xsl:if>
    </schedule>
  </xsl:template>
  
  <xsl:template match="pm_completion">
    <xsl:attribute name="progress">
      <xsl:value-of select="."/>
    </xsl:attribute>
  </xsl:template>
  
  <xsl:template match="pm_modified | tr_modified | info_datemodified">
    <xsl:attribute name="modified">
      <xsl:value-of select="f:secondsToDateTime(.)"/>
    </xsl:attribute>
  </xsl:template>
  
  <xsl:template match="tr_closed">
    <xsl:attribute name="closed">
      <xsl:value-of select="f:secondsToDateTime(.)"/>
    </xsl:attribute>
  </xsl:template>
  
  <xsl:template match="info_startdate">
    <schedule start="{f:secondsToDateTime(.)}">
      <xsl:if test="../info_enddate > 0">
        <xsl:attribute name="end">
          <xsl:value-of select="f:secondsToDateTime(../info_enddate)"/>
        </xsl:attribute>
      </xsl:if>
    </schedule>
  </xsl:template>
  
  <xsl:function name="f:secondsToDateTime" as="xs:dateTime">
    <xsl:param name="secs"/>
    <xsl:variable name="one-sec" select="xs:time('00:00:01') - xs:time('00:00:00')"/>
    <xsl:sequence select="xs:dateTime('1970-01-01T00:00:00') + xs:integer($secs) * $one-sec"/>
  </xsl:function>
  
</xsl:stylesheet>
